package com.muqi.app.project.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.muqi.app.qlearn.modles.UserInfo;

import android.content.Context;
import android.graphics.Bitmap;

public class CustomerUtil {
	/**
	 * 保存用户信息到本地 openFileOutput方法将文件保存在/data/data/<package name>/files目录下
	 */
	public static void saveUserInfo(Context context, UserInfo userInfo) {
		deleteUserInfo(context);
		FileOutputStream fos = null;
		try {
			fos = context.openFileOutput("userInfo.out", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(userInfo);// 写入
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null) {
					fos.close();// 关闭输出流
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 从本地获取用户信息
	 */
	public static UserInfo getUserInfo(Context context) {
		UserInfo uInfo = null;
		try {
			FileInputStream fis = context.openFileInput("userInfo.out"); // 获得输入流
			ObjectInputStream ois = new ObjectInputStream(fis);
			uInfo = (UserInfo) ois.readObject();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return uInfo;
	}

	/**
	 * 从本地删除用户信息
	 */
	public static void deleteUserInfo(Context context) {
		File f = new File(context.getFilesDir(), "userInfo.out");
		if (f.exists()) {
			f.delete();
		}
	}

	/**
	 * 判断用户信息是否存在
	 */
	public static boolean existUserInfo(Context context) {
		File f = new File(context.getFilesDir(), "userInfo.out");
		if (f.exists()) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * 二维码生成工具
	 * 
	 * @param url
	 * @param QR_WIDTH
	 * @param QR_HEIGHT
	 * @return
	 */
	public static Bitmap createQRImage(String url, int QR_WIDTH, int QR_HEIGHT) {
		try {
			if (url == null || "".equals(url) || url.length() < 1) {// 判断URL合法性
				return null;
			}
			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			// hints.put(EncodeHintType.MARGIN, 2); //default is 4
			// 图像数据转换，使用了矩阵转换
			BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
			int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
			// 下面这里按照二维码的算法，逐个生成二维码的图片，
			// 两个for循环是图片横列扫描的结果
			for (int y = 0; y < QR_HEIGHT; y++) {
				for (int x = 0; x < QR_WIDTH; x++) {
					if (bitMatrix.get(x, y)) {
						pixels[y * QR_WIDTH + x] = 0xff000000;
					} else {
						pixels[y * QR_WIDTH + x] = 0x00ffffff;
					}
				}
			}
			// 生成二维码图片的格式，使用ARGB_8888
			Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
			bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
			// 显示到一个ImageView上面
			return bitmap;
		} catch (WriterException e) {
			e.printStackTrace();
		}
		return null;
	}
}
